package com.cpems.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cpems.common.core.page.TableDataInfo;
import com.cpems.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cpems.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cpems.system.domain.bo.EnergyProjectSyncBo;
import com.cpems.system.domain.vo.EnergyProjectSyncVo;
import com.cpems.system.domain.EnergyProjectSync;
import com.cpems.system.mapper.EnergyProjectSyncMapper;
import com.cpems.system.service.IEnergyProjectSyncService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目总能效同步Service业务层处理
 *
 * @author lyonfive
 * @date 2025-03-26
 */
@RequiredArgsConstructor
@Service
public class EnergyProjectSyncServiceImpl implements IEnergyProjectSyncService {

    private final EnergyProjectSyncMapper baseMapper;

    @Override
    public TableDataInfo<EnergyProjectSync> selectPageEnergySyncList(EnergyProjectSyncBo bo, PageQuery pageQuery) {
        Page<EnergyProjectSync> page = baseMapper.selectPageEnergySyncList(pageQuery.build(), this.buildQueryWrapperMapper(bo));
        return TableDataInfo.build(page);
    }

    /**
     * 查询项目总能效同步
     */
    @Override
    public EnergyProjectSyncVo queryById(Long energyId){
        return baseMapper.selectVoById(energyId);
    }

    /**
     * 查询项目总能效同步列表
     */
    @Override
    public TableDataInfo<EnergyProjectSyncVo> queryPageList(EnergyProjectSyncBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EnergyProjectSync> lqw = buildQueryWrapper(bo);
        Page<EnergyProjectSyncVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目总能效同步列表
     */
    @Override
    public List<EnergyProjectSyncVo> queryList(EnergyProjectSyncBo bo) {
        LambdaQueryWrapper<EnergyProjectSync> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EnergyProjectSync> buildQueryWrapper(EnergyProjectSyncBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EnergyProjectSync> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCurrentCop() != null, EnergyProjectSync::getCurrentCop, bo.getCurrentCop());
        lqw.eq(bo.getMonthCop() != null, EnergyProjectSync::getMonthCop, bo.getMonthCop());
        lqw.eq(bo.getTotalCop() != null, EnergyProjectSync::getTotalCop, bo.getTotalCop());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), EnergyProjectSync::getRemark, bo.getRemark());
        lqw.eq(ObjectUtil.isNotNull(bo.getProjectId()), EnergyProjectSync::getProjectId, bo.getProjectId());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,EnergyProjectSync::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return lqw;
    }

    private Wrapper<EnergyProjectSync> buildQueryWrapperMapper(EnergyProjectSyncBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<EnergyProjectSync> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(bo.getProjectId()), "e.project_id", bo.getProjectId())
        .eq(ObjectUtil.isNotNull(bo.getTypeId()), "e.type_id", bo.getTypeId())
        .between(params.get("beginTime") != null && params.get("endTime") != null,"e.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }

    /**
     * 新增项目总能效同步
     */
    @Override
    public Boolean insertByBo(EnergyProjectSyncBo bo) {
        EnergyProjectSync add = BeanUtil.toBean(bo, EnergyProjectSync.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEnergyId(add.getEnergyId());
        }
        return flag;
    }

    /**
     * 修改项目总能效同步
     */
    @Override
    public Boolean updateByBo(EnergyProjectSyncBo bo) {
        EnergyProjectSync update = BeanUtil.toBean(bo, EnergyProjectSync.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EnergyProjectSync entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目总能效同步
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
